//
//  ImageHandleView.m
//  08-画板
//
//  Created by J on 16/3/25.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ImageHandleView.h"
@interface ImageHandleView()<UIGestureRecognizerDelegate>
@property(nonatomic,strong)UIImageView* imageView;
@end
@implementation ImageHandleView

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _imageView.userInteractionEnabled = YES;
        [self addSubview:_imageView];
        
        [self addGesture];//添加手势
        
    }
    return _imageView;
}



- (void)addGesture{
    //平移
    UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self  action:@selector(pan:)];
    [_imageView addGestureRecognizer:pan];
    //旋转
    UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    [_imageView addGestureRecognizer:rotation];

    //缩放
    UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.delegate = self;
    [_imageView addGestureRecognizer:pinch];

    //长按
    UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [_imageView addGestureRecognizer:longPress];

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;//同时支持多个手势 就是多个手势无缝切换
}


- (void)longPress:(UILongPressGestureRecognizer*)longPress{
    if (longPress.state == UIGestureRecognizerStateBegan) {//图片处理完毕
        //图片处理完毕
        
        //高亮效果   alpha=0 -> alpha =1 -> saveImage
        [UIView animateWithDuration:0.25 animations:^{
            
            self.imageView.alpha = 0;
        }completion:^(BOOL finished) {
            [UIView animateWithDuration:0.25 animations:^{
                self.imageView.alpha = 1;
                
            }completion:^(BOOL finished) {
                //高亮完成的时候 图片处理完毕 截屏
                //把处理的图片生成一张新的图片
                
                [self saveScreen];

            }];
            
        }];
        
        
        
    }
    
//    NSLog(@"longPress");
    
}

- (void)saveScreen{
    //这里要保存当前整个透明背景view的大小
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);//opaque 不透明度设为NO  不是不透明就是透明
    CGContextRef ref =  UIGraphicsGetCurrentContext();

    //把 当前  控件图层渲染到上下文
    [self.layer renderInContext:ref];
    
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    //把图片传给drawView
    if (self.handleCompletionBlock) {
        self.handleCompletionBlock(image);
    }
    //销毁当前控件
    [self removeFromSuperview];
    
    
    UIGraphicsEndImageContext();//关闭上下文
}


- (void)pinch:(UIPinchGestureRecognizer*)pinch{
    
//    NSLog(@"pinch");
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;//复位
}

- (void)rotation:(UIRotationGestureRecognizer*)rotation{
    
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
    rotation.rotation = 0;//复位
    
//    NSLog(@"rotation");
    
}


- (void)pan:(UIPanGestureRecognizer*)pan{
    
   CGPoint point =  [pan translationInView:self.imageView];
    //设置UIImageView形变
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
    //复位:只要想要相对于上一次就必须复位
    [pan setTranslation:CGPointZero inView:self.imageView];
//    NSLog(@"pan");
    
}




- (void)setImage:(UIImage *)image{
    _image = image;
    
    //把图片展示到UIImageView上
    self.imageView.image = image;

    
}

@end
